package b.i.d;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Build.VERSION;
import android.os.Handler;
import android.util.Log;
import b.f.f;
import b.f.h;
import b.i.c.b.b;
import b.i.c.b.e;
import b.i.c.b.g;
import b.i.h.a;
import b.i.h.c;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Objects;

@SuppressLint({"NewApi"})
/* compiled from: TypefaceCompat */
public class d {
    public static final k a;
    public static final f<String, Typeface> b = new f(16);

    static {
        int i = VERSION.SDK_INT;
        if (i >= 29) {
            a = new i();
        } else if (i >= 28) {
            a = new h();
        } else if (i >= 26) {
            a = new g();
        } else {
            if (i >= 24) {
                Method method = f.d;
                if (method == null) {
                    Log.w("TypefaceCompatApi24Impl", "Unable to collect necessary private methods.Fallback to legacy implementation.");
                }
                if ((method != null ? 1 : null) != null) {
                    a = new f();
                }
            }
            a = new e();
        }
    }

    public static Typeface a(Context context, b bVar, Resources resources, int i, int i2, g gVar, Handler handler, boolean z) {
        Typeface typeface;
        if (bVar instanceof e) {
            e eVar = (e) bVar;
            Object obj = 1;
            if (z ? eVar.c != 0 : gVar != null) {
                obj = null;
            }
            int i3 = z ? eVar.b : -1;
            a aVar = eVar.a;
            f fVar = b.i.h.e.a;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(aVar.e);
            stringBuilder.append("-");
            stringBuilder.append(i2);
            String stringBuilder2 = stringBuilder.toString();
            typeface = (Typeface) b.i.h.e.a.a(stringBuilder2);
            if (typeface != null) {
                if (gVar != null) {
                    gVar.d(typeface);
                }
            } else if (obj == null || i3 != -1) {
                b.i.h.b bVar2 = new b.i.h.b(context, aVar, i2, stringBuilder2);
                typeface = null;
                if (obj != null) {
                    try {
                        typeface = ((b.i.h.e.d) b.i.h.e.b.b(bVar2, i3)).a;
                    } catch (InterruptedException unused) {
                    }
                } else {
                    Object obj2;
                    if (gVar == null) {
                        obj2 = typeface;
                    } else {
                        obj2 = new c(gVar, handler);
                    }
                    synchronized (b.i.h.e.c) {
                        h hVar = b.i.h.e.d;
                        ArrayList arrayList = (ArrayList) hVar.getOrDefault(stringBuilder2, typeface);
                        if (arrayList != null) {
                            if (obj2 != null) {
                                arrayList.add(obj2);
                            }
                        } else {
                            if (obj2 != null) {
                                arrayList = new ArrayList();
                                arrayList.add(obj2);
                                hVar.put(stringBuilder2, arrayList);
                            }
                            b.i.h.f fVar2 = b.i.h.e.b;
                            b.i.h.d dVar = new b.i.h.d(stringBuilder2);
                            Objects.requireNonNull(fVar2);
                            fVar2.a(new b.i.h.g(fVar2, bVar2, new Handler(), dVar));
                        }
                    }
                }
            } else {
                b.i.h.e.d b = b.i.h.e.b(context, aVar, i2);
                if (gVar != null) {
                    int i4 = b.b;
                    if (i4 == 0) {
                        gVar.b(b.a, handler);
                    } else {
                        gVar.a(i4, handler);
                    }
                }
                typeface = b.a;
            }
        } else {
            typeface = a.a(context, (b.i.c.b.c) bVar, resources, i2);
            if (gVar != null) {
                if (typeface != null) {
                    gVar.b(typeface, handler);
                } else {
                    gVar.a(-3, handler);
                }
            }
        }
        if (typeface != null) {
            b.b(c(resources, i, i2), typeface);
        }
        return typeface;
    }

    public static Typeface b(Context context, Resources resources, int i, String str, int i2) {
        Typeface d = a.d(context, resources, i, str, i2);
        if (d != null) {
            b.b(c(resources, i, i2), d);
        }
        return d;
    }

    public static String c(Resources resources, int i, int i2) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(resources.getResourcePackageName(i));
        String str = "-";
        stringBuilder.append(str);
        stringBuilder.append(i);
        stringBuilder.append(str);
        stringBuilder.append(i2);
        return stringBuilder.toString();
    }
}
